<?php
/**
 * Created by PhpStorm.
 * User: tangsian
 * Date: 2020/12/17
 * Time: 18:12
 */

namespace App\Http\Controllers;


use App\Http\Model\Api\FinanceRoomWealthStream;
use App\Http\Model\Api\Room;
use App\Http\Model\Api\Union;
use App\Http\Model\AudioApi;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\SoundByte;
use App\Http\Services\ConfigServices;
use App\Http\Services\SoundByteServices;
use Illuminate\Http\Request;

class SoundByteController extends Controller
{
    protected $configServices;
    protected $roomModel;
    protected $unionModel;
    protected $soundByteModel;
    protected $financeRoomWealthModel;
    protected $audioApi;
    protected $soundByteServices;

    public function __construct()
    {
        parent::__construct();
        $this->configServices = new ConfigServices();
        $this->roomModel = new Room();
        $this->unionModel = new Union();
        $this->soundByteModel = new SoundByte();
        $this->financeRoomWealthModel = new FinanceRoomWealthStream();
        $this->audioApi = new AudioApi();
        $this->soundByteServices = new SoundByteServices();
    }

    /*
     * 拉取赠送声浪获得声浪值
     */
    public function assignAction($body)
    {
        $gift_id = $body['gift_id'];
        $room_id = $body['room_id'];    //房间Id
        $user_id = $body['user_id'];    // 送礼物者
        $to_user_id_array = $body['to_user_id_array'];  // 收礼物者
        $gift_num = $body['gift_num'];
        $body_date = $body['create']; //送礼物时间
        $consumer_date = $body_date ?? time();
        $checkDate = $this->configServices->checkConfigDateNew(26, $consumer_date, 'sound_byte_date');
        if (!$checkDate) {
            write_log('soundByteConsumer', 'timeError', $body);//活动时间错误
            return false;
        }

        //测试服369 线上服332
        $gift_id_arr = [332];
        //gift_id 指定礼物
        if (!in_array($gift_id, $gift_id_arr)) {
            write_log('soundByteConsumer', 'giftError', "非指定礼物");
            return false;
        }

        //提升送礼物个人神豪排行数据
        $this->soundByteModel->setSendUserRank($user_id, $to_user_id_array, $gift_num);

        //判断房间流水
        $roomWealthData = $this->audioApi->getRoomWealth($room_id);
        if (!empty($roomWealthData['data']['wealth_value'])) {
            if ($roomWealthData['data']['wealth_value'] > 500000 && $roomWealthData['data']['wealth_value'] < 1000000) {
                $gift_num += ceil($gift_num / 2);
            } elseif ($roomWealthData['data']['wealth_value'] > 1000000) {
                $gift_num = 2 * $gift_num;
            }
        }

        $roomInfo = $this->roomModel->getRoomInfo($room_id);
        if ($roomInfo['business_type'] == 0) {
            foreach ($to_user_id_array as $userVal) {
                //提升小窝声浪排行数据
                $this->soundByteModel->setSmallHouseSoundByteByType($userVal, $gift_num);
            }
            return true;
        }

        //判断被赠送用户是否符合 （在自己签约的工会房间内）
        $roomFamilyInfo = $this->roomModel->getRoomFamilyInfo($room_id);
        $unionSignInfo = $this->unionModel->getUserUnionSignInfo($to_user_id_array);
        $eligibleArr = [];

        foreach ($unionSignInfo as $val) {
            if ($val['expire_time'] < time()) {
                continue;
            }
//            if ($val['uuid'] != $roomFamilyInfo['family_uid']) {
//                continue;
//            }
//            $eligibleArr[] = $val['user_id'];
            //提升个人音浪排行数据
            if (in_array($roomFamilyInfo['room_type'], [1, 4])) {
                $this->soundByteModel->setPersonalSoundByteByType($roomFamilyInfo['room_type'], $val['user_id'], $gift_num);
            }

        }
        foreach ($to_user_id_array as $userVal){
            if(!empty($roomFamilyInfo['family_uid'])){
                //提升工会音浪排行数据
                if ($roomFamilyInfo['business_type'] != 0) {
                    $this->soundByteModel->setFamilySoundByte($roomFamilyInfo['family_uid'], $room_id, $gift_num);
                }
            }
        }

        return true;
    }

    /*
     * 获取工会声浪排行
     */
    public function soundByteFamilyList(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $page = $request->input('page') ?? 0;
            $limit = $request->input('limit') ?? 10;
            $user_id = $request->input('user_id');

            $checkDate = $this->configServices->checkConfigDate(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $data = $this->soundByteServices->soundByteFamilyList($page, $limit,$user_id);
        } while (false);
        return compact('code', 'msg', 'data');
    }

    /*
     * 根据房间类型获取私人声浪排行
     */
    public function soundBytePersonalListByType(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $page = $request->input('page') ?? 0;
            $limit = $request->input('limit') ?? 10;
            $user_id = $request->input('user_id');
            $type = $request->input('type') ?? 0;

            if (empty(intval($type))) {
                $code = Hint::PARAM_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }

            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $data = $this->soundByteServices->soundBytePersonalListByType($type, $user_id, $page, $limit);
        } while (false);
        return compact('code', 'msg', 'data');
    }

    /*
     * 设置暂停一天，并生成晋级人员数据。
     */
    public function setAdvanced()
    {
        $result = $this->soundByteModel->setSuspendTime();
        if ($result == true) {
            $typeArr = [1, 4];
            $this->soundByteModel->setAdvancedList($typeArr);
        }
    }

    public function setSuspendTime(){
        $time = 86400*60;
        $result = $this->soundByteModel->setSuspendTime($time);
    }

    public function getSoundByteStep(){
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $data['step']=$this->soundByteModel->getTimeStep();
        }while(false);

        return compact('code', 'msg', 'data');
    }

    /*
     * 获取聚贤庄排行榜
     */
    public function getFriendsCountRank(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $page = $request->input('page') ?? 0;
            $limit = $request->input('limit') ?? 10;
            $user_id = $request->input('user_id');

            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $dataResult = $this->soundByteServices->getFriendsCountRank($user_id);
            $data['my_rank_data'] = $dataResult['my_rank_data'];
            $data['total_count'] = $dataResult['total_count'];
            $data['rank_list'] = array_slice($dataResult['rank_list'], $page * $limit, $limit);

        } while (false);
        return compact('code', 'msg', 'data');

    }

    //定时设置当前阶段粉丝排行
    public function setFriendsCountRank()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $data = $this->soundByteServices->setFriendsCountRank();

        } while (false);
        return compact('code', 'msg', 'data');
    }

    //定时设置暂停时间，并插入对应前阶段数据
    public function setFriendsSuspendTime(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {

            $num = $request->input('num') ?? 0;
            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $result = $this->soundByteServices->setFriendsSuspendTime($num);
            if (!$result) {
                $code = Hint::ERR_FAIL;
                $msg = '比赛进行期间不能暂停';
                break;
            }

        } while (false);
        return compact('code', 'msg', 'data');
    }

    /*
    * 获取神豪排行榜
    */
    public function getDiamondCountRank(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $page = $request->input('page') ?? 0;
            $limit = $request->input('limit') ?? 10;
            $user_id = $request->input('user_id');

            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $data = $this->soundByteServices->soundByteSendUserList($user_id, $page, $limit);

        } while (false);
        return compact('code', 'msg', 'data');

    }

    //定时设置当前阶段神豪排行
    public function setDiamondCountRank()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $data = $this->soundByteServices->setDiamondCountRank();

        } while (false);
        return compact('code', 'msg', 'data');
    }

    //定时设置暂停时间，并插入对应前阶段数据
    public function setDiamondSuspendTime(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {

            $num = $request->input('num') ?? 0;
            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $result = $this->soundByteServices->setDiamondSuspendTime($num);
            if (!$result) {
                $code = Hint::ERR_FAIL;
                $msg = '比赛进行期间不能暂停';
                break;
            }

        } while (false);
        return compact('code', 'msg', 'data');
    }

    /*
    * 获取窝神排行榜
    */
    public function getSmallHouseCountRank(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $page = $request->input('page') ?? 0;
            $limit = $request->input('limit') ?? 10;
            $user_id = $request->input('user_id');

            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $data = $this->soundByteServices->soundByteSmallHouseList($user_id, $page, $limit);

        } while (false);
        return compact('code', 'msg', 'data');

    }

    /*
    * 获取亲密值排行榜
    */
    public function getIntimacyCountRank(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $page = $request->input('page') ?? 0;
            $limit = $request->input('limit') ?? 10;
            $user_id = $request->input('user_id');

            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $dataResult = $this->audioApi->intimacyRank($user_id,$page,$limit);
            $data = $dataResult['data'];
            $code = $dataResult['code'];
            $msg = $dataResult['msg'];
//            $dataResult = $this->soundByteServices->getIntimacyCountRank($user_id);
//
//            $data['my_rank_data'] = $dataResult['my_rank_data'];
//            $data['total_count'] = $dataResult['total_count'];
//            $data['rank_list'] = array_slice($dataResult['rank_list'], $page * $limit, $limit);
        } while (false);
        return compact('code', 'msg', 'data');

    }

    //定时设置当前阶段亲密值排行
    public function setIntimacyCountRank()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $data = $this->soundByteServices->setIntimacyCountRank();

        } while (false);
        return compact('code', 'msg', 'data');
    }

    //定时设置暂停时间，并插入对应前阶段数据
    public function setIntimacySuspendTime(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {

            $num = $request->input('num') ?? 0;
            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $result = $this->soundByteServices->setIntimacySuspendTime($num);
            if (!$result) {
                $code = Hint::ERR_FAIL;
                $msg = '比赛进行期间不能暂停';
                break;
            }

        } while (false);
        return compact('code', 'msg', 'data');
    }

    /*
    * 获取亲密值排行榜
    */
    public function getRoomDurationCountRank(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $page = $request->input('page') ?? 0;
            $limit = $request->input('limit') ?? 10;
            $user_id = $request->input('user_id');

            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $dataResult = $this->soundByteServices->getRoomDurationCountRank($user_id);

            $data['my_rank_data'] = $dataResult['my_rank_data'];
            $data['total_count'] = $dataResult['total_count'];
            $data['rank_list'] = array_slice($dataResult['rank_list'], $page * $limit, $limit);
        } while (false);
        return compact('code', 'msg', 'data');

    }

    //定时设置当前阶段亲密值排行
    public function setRoomDurationCountRank()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $data = $this->soundByteServices->setRoomDurationCountRank();

        } while (false);
        return compact('code', 'msg', 'data');
    }

    //定时设置暂停时间，并插入对应前阶段数据
    public function setRoomDurationSuspendTime(Request $request)
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {

            $num = $request->input('num') ?? 0;
            $checkDate = $this->configServices->checkConfigDateNew(26, time(), 'sound_byte_date');
            if (!$checkDate) {
                $code = Hint::ERR_FAIL;
                $msg = '不在活动期间';
                break;
            }

            $result = $this->soundByteServices->setRoomDurationSuspendTime($num);
            if (!$result) {
                $code = Hint::ERR_FAIL;
                $msg = '比赛进行期间不能暂停';
                break;
            }

        } while (false);
        return compact('code', 'msg', 'data');
    }
}
